﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormMultiLang
{
    [ProvideProperty("ResourceName", typeof(IComponent))]
    public partial class LanguageProvider : Component, IExtenderProvider
    {
        private Dictionary<IComponent, string> resources = null;

        public LanguageProvider()
        {
            resources = new Dictionary<IComponent, string>();
        }

        public LanguageProvider(IContainer container)
            : this()
        {
            container.Add(this);
        }

        public bool CanExtend(object extendee)
        {
            return extendee is IComponent;
        }

        /// <summary>
        /// 获取控件所对应的资源名称。
        /// </summary>
        /// <param name="control"></param>
        /// <returns></returns>
        public string GetResourceName(IComponent control)
        {
            if (resources.ContainsKey(control))
            {
                return resources[control];
            }

            return string.Empty;
        }

        /// <summary>
        /// 设置控件所对应的资源名称。
        /// </summary>
        /// <param name="control"></param>
        /// <param name="resourceName"></param>
        public void SetResourceName(IComponent control, string resourceName)
        {
            if (resources.ContainsKey(control))
            {
                if (string.IsNullOrEmpty(resourceName))
                {
                    resources.Remove(control);
                }
                else
                {
                    resources[control] = resourceName;
                }
            }
            else if (!string.IsNullOrEmpty(resourceName))
            {
                resources.Add(control, resourceName);
            }
        }
    }
}
